// pages/reservelist/reservelist.js
import {getReserveList,bindUser,bindMobild} from '../../http/protocol'
import {checkLogin,getUserInfo,updatecode,getLogin,getSetting} from '../../const/util'
import {appkey,appId} from '../../const/common';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    resultList: [],
    login: false,
    type:'user'
  },

  /**
   * 生命周期函数--监听页面加载
   */
   onLoad: async function (options) {
    let setting = await getSetting(); // 是否授权
    if(!!setting ) {
     let userInfo = await getUserInfo();
     this.bindUserInfo({detail: userInfo});
    } else {
     //  未授权的话 跳准到个人中心
     wx.redirectTo({
       url: '/pages/ucenter/ucenter',
     })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  async bindMobile(e) {
    let {detail, detail : {encryptedData}} = e;
    if(detail.errMsg === "getPhoneNumber:ok") {
    let code = await getLogin();
   let res = await bindMobild({
      appkey,   code, data: JSON.stringify(detail),
    })
    if(res.success) {
      // 绑定手机号后 更新code
      wx.showToast({
        title: "授权登录成功",
        icon:'none'
      })
      updatecode(()=> {
        this.getReverseList1()
      });
    } else {
      // wx.showToast({
      //   title: res.msg,
      //   icon:'none'
      // })
    }

  }
  },

  async bindUserInfo(e){
    let {detail,detail:{encryptedData,rawData,signature,iv,errMsg,userInfo}} = e;
    if(errMsg === 'getUserInfo:ok') {
      let code = await getLogin();
      // wx.showLoading()
      let res = await bindUser({appkey, code,
        data: JSON.stringify({encryptedData, rawData,signature,iv})
      })
      if(res.success) {
        let data =res.data;
        let {memberEncode, bindMobile} = data;
        wx.setStorageSync('memberEncode', memberEncode);
        wx.setStorageSync('bindMobile', bindMobile);
        if(bindMobile === 'Y') {
          this.getReverseList1()
        } else {
          // this.setData({type:'mobile'})
          wx.redirectTo({
            url: '/pages/ucenter/ucenter',
          })
        }
      } else {
        // wx.showToast({
        //   title: res.msg,
        //   icon:'none'
        // })
      }
      // wx.hideLoading()
    }
  },

  gotod(e){
    try {
      let orderNo = e.currentTarget.dataset.number;
      wx.navigateTo({
        url: `/pages/reservedetail/reservedetail?orderNo=${orderNo}`,
      })
    } catch (error) {
      
    }
 
  },

  async getReverseList1(){
    let res1 = await getReserveList();
    if( res1.success ) {
      let list = res1.data.resultList;
      this.setData({resultList:list})
    } else {
      wx.showToast({
        title: res1.msg,
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
   onShow: async function () {
    //  let checkLogin1 = await checkLogin();
    //  let bindMobileText =  wx.getStorageSync('bindMobile');
    //  if( !checkLogin1 ) return
    //  if(!bindMobileText ) return
    //  if( bindMobileText === "N" ) return

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})